#include "TutorialState.h"
#include "Game.h"
#include "MainMenuState.h"
#include "Player.h"
#include "ObjectManager.h"
#include "TileSystem.h"
#include "Camera.h"
#include "../SGD Wrappers/SGD_InputManager.h"

#include "MessageID.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

TutorialState* TutorialState::GetInstance()
{
	static TutorialState s_instance;

	return &s_instance;
}

void TutorialState::Enter()
{
	// Initialize the Message Manager
	m_pMessages = SGD::MessageManager::GetInstance();
	m_pMessages->Initialize(&MessageProc);

	m_pEvents = SGD::EventManager::GetInstance();
	m_pEvents->Initialize();

	m_pGraphics = SGD::GraphicsManager::GetInstance();

	m_pPlayer = new Player;
	m_pPlayer->SetImage(m_pGraphics->LoadTexture(L"resource/graphics/Scientist.png"));
	m_pPlayer->SetSize({ 64, 128 });

	m_pPlayer->SetPosition({ 200, 200 });
	m_pCamera = new Camera(m_pPlayer);
	Game::GetInstance()->SetCamera(m_pCamera);


	m_pObjects = new ObjectManager();

	m_pObjects->GetTileSystem()->Load("resource/graphics/TestXY.xml", m_pObjects);

}

void TutorialState::Exit()
{
	m_pObjects->RemoveAll();
	delete m_pObjects;
	delete m_pPlayer;


	// Terminate & deallocate the SGD wrappers
	m_pEvents->Terminate();
	m_pEvents = nullptr;
	SGD::EventManager::DeleteInstance();

	m_pMessages->Terminate();
	m_pMessages = nullptr;
	SGD::MessageManager::DeleteInstance();
}

void TutorialState::Update(float dt)
{
	m_pObjects->UpdateAll(dt);
	m_pPlayer->Update(dt);
	m_pCamera->Update(dt);

	m_pObjects->CheckCollisions(m_pPlayer);

}

void TutorialState::Render()
{
	m_pObjects->RenderAll();
	m_pPlayer->Render();
}

bool TutorialState::Input()
{
	SGD::InputManager* input = SGD::InputManager::GetInstance();
	if (input->IsKeyPressed(SGD::Key::M))
		Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
	return true;
}

void TutorialState::MessageProc(const SGD::Message* pMsg)
{
	/* Show warning when a Message ID enumerator is not handled */
#pragma warning( push )
#pragma warning( 1 : 4061 )

	// What type of message?
	switch (pMsg->GetMessageID())
	{
	case MessageID::MSG_UNKNOWN:
	default:
		OutputDebugStringW(L"Game::MessageProc - unknown message id\n");
		break;
	case MessageID::MSG_PLAYER_DIED:
	{
		TutorialState::GetInstance()->m_pPlayer->SetPosition({ 100, 300 });
		TutorialState::GetInstance()->m_pObjects->Respawn(0);
	}
		break;
	case MessageID::MSG_RESETBOMB:
		break;
	}


	/* Restore previous warning levels */
#pragma warning( pop )
}